class Watcher {
  /**
   * addWatcher(vm, watcher, key)
   * this.watcher -> watch
   * {
   *   key: ?,
   *   fn: keyFn
   * }
   */
  constructor() {
    this.watchers = [];
  }

  addWatcher(vm, watcher, key) {
    this._addWatchProp({
      key,
      fn: watcher[key].bind(vm)
    });

    // console.log(this.watchers);
    /**
     * [
     *   {key: "total", fn: ƒ},
     *   {key: "a", fn: ƒ},
     *   {key: "b", fn: ƒ}
     * ]
     */
  }

  //调用
  invoke(key, newValue, oldValue) {
    this.watchers.map(item => {
      if (item.key === key) {
        item.fn(newValue, oldValue);
      }
    })
  }

  _addWatchProp(watchProp) {
    this.watchers.push(watchProp);
  }
}

export default Watcher;